package com.excesys.exsecs.energy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.energy.dto.BumDevListDto;
import com.excesys.exsecs.energy.entity.BumDevList;
import com.excesys.exsecs.energy.service.BumDevListService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供配电配电室设备映射
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/devList")
@Tag(name = "供配电配电室设备映射")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BumDevListController {

	private final BumDevListService bumDevListService;

//	/**
//	 * 分页查询
//	 * @param page 分页对象
//	 * @param bumDevList
//	 * @return
//	 */
//	@Operation(summary = "分页查询", description = "分页查询")
//	@GetMapping("/page")
//	public R getPage(Page page, BumDevList bumDevList) {
//		return R.ok(bumDevListService.page(page, Wrappers.query(bumDevList).orderByAsc("create_time")));
//	}

	/**
	 * 新增
	 * @param bumDevList
	 * @return
	 */
	@Operation(summary = "新增", description = "新增")
	@PostMapping
	public R save(@RequestBody BumDevList bumDevList) {
		return R.ok(bumDevListService.save(bumDevList));
	}

	/**
	 * 修改
	 * @param bumDevList
	 * @return
	 */
	@Operation(summary = "修改", description = "修改")
	@PutMapping
	public R edit(@RequestBody BumDevList bumDevList) {
		return R.ok(bumDevListService.updateById(bumDevList));
	}

	/**
	 * 删除
	 * @param bumDevList
	 * @return
	 */
	@Operation(summary = "删除", description = "删除")
	@DeleteMapping
	public R del(BumDevList bumDevList) {
		List<BumDevList> devListList = bumDevListService.queryDelDevById(Long.valueOf(bumDevList.getId()));
		if(devListList.size()>0){
			for(BumDevList bumDevList1:devListList){
				bumDevListService.removeById(bumDevList1.getId());
			}
		}
		return R.ok(bumDevListService.removeById(bumDevList.getId()));
	}

	/**
	 * 通过配电室id查询绑定设备
	 */
	@Operation(summary = "通过配电室id查询绑定设备", description = "通过配电室id查询绑定设备")
	@GetMapping("/queryDevById")
	public R queryDevById(Page page, BumDevList bumDevList) {
		List<BumDevListDto> devListDtoList = bumDevListService.queryDevById(bumDevList);
		Page pages = bumDevListService.setPage(devListDtoList, page);
		return R.ok(pages);
	}


}